<?php

/**
 * @author Anh Nguyen <duyanhnguyen@vccorp.vn>
 * @copyright 2012
 */
class Network_model extends CI_Model {

    function __construct() {
        parent::__construct();
    }

    public function getAllNetwork($number, $offset, $keywork) {
        $return = array();
        $this->db->select('*')
                ->from('list_network')
                ->order_by('name')
                ->like('name', $keywork)
                 ->limit($number, $offset);
        $rs = $this->db->get();
        $return = $rs->result_array();
        $rs->free_result();
        return $return;
    }
    
    public function getInfo($id)
    {
        $return = array();
        $this->db->select('A.id, A.name')
                ->from('list_network A')
                ->where('A.id', $id);
        $rs = $this->db->get();
        $return = $rs->result_array();
        $rs->free_result();
        return $return;
    }
    
    public function getFullNetwork()
    {
        $return = array();
        $this->db->select('*')
                ->from('list_network')
                ->order_by('name');
        $rs = $this->db->get();
        $return = $rs->result_array();
        $rs->free_result();
        return $return;
    }
    
    function count_all($keywork){
        if($keywork != '') {
           return $this->db->select('name')
                    ->from('list_network')
                    ->like('name', $keywork)
                    ->count_all_results();
        }
			return $this->db->count_all('list_network');
		}

    public function updateNameNetwork($Id, $data = array()) {
        if (!is_null($data)) {
            $result = $this->db->update('list_network', $data, array(
                'id' => $Id,
            ));
            return true;
        }
        return false;
    }

    public function updatePriceNetwork($typeGroup, $Id, $price_update) {
        $sql = "UPDATE `item_sim` SET `price` = `price_update`, `price_update` = `price_update` + $price_update WHERE $typeGroup = $Id";
        if ($this->db->query($sql))
            return true;
        return false;
    }
    public function checkNetworkExists($list_network_id)
    {
        $this->db->select('*')
                ->from('list_network')
                ->where('id', $list_network_id);
        $rs = $this->db->get();
        $return = ($rs->num_rows() > 0) ? true : false;
        $rs->free_result();
        return $return;
    }

    public function deleteNameNetwork($Id) {
        $this->db->delete('list_network', array('id' => $Id));
    }

    public function updateNameNetworkDeleteF($Id, $data = array()) {
        if (!is_null($data)) {
            $this->db->update('list_network', $data, array(
                'id' => $Id
            ));
        }
    }
    
    public function checkNExists($Id)
    {
        $this->db->select('S.phone')
                ->from('item_sim S')
                ->join('list_network N', 'S.list_network_id = N.id', 'left')
                ->where('N.id', $Id);
        $rs = $this->db->get();
        $return = ($rs->num_rows() > 0) ? true : false;
        $rs->free_result();
        return $return;
    }

    public function networkExists($name) {
        $this->db->select('*');
        $this->db->from('list_network');
        $this->db->where('name', $name);
        $rs = $this->db->get();
        $return = ($rs->num_rows() > 0) ? true : false;
        $rs->free_result();
        return $return;
    }

    public function addNameNetwork($name) {
        $array = array(
//            'parent_id' => $parent_id,
            'name' => $name,
//            'name_en' => $name_en,
//            'href' => $href,
            'delete_flag' => '0'
        );
        return $this->db->insert('list_network', $array);
    }
    
     public function addNetworkBatch($data) {
        return $this->db->insert_batch('list_network', $data);
    }

    public function getInfoSearch($kw){
        $return = array();
        $this->db->select('*')
                ->from('list_network')
//                ->order_by('name')
                ->like('name',$kw);
        $rs = $this->db->get();
        $return = $rs->result_array();
        $rs->free_result();
        
        return $return;
    }
    
}

?>
